<template>
  <div :style="[{ height: $attrs.height - 100 + 'px' }]">
    <div :style="[{ 'overflow-y': 'auto' }, { height: $attrs.height - 150 + 'px' }]">
      <el-collapse v-model="activeName" accordion>
        <el-collapse-item name="1">
          <template #title>
            <div class="coll-title">基本信息</div>
          </template>
          <el-form ref="form" :model="taskForm" :rules="rules" label-width="120px" class="selfDxFrom">
            <el-form-item label="任务编号" prop="code">
              <el-input v-model="taskForm.code" placeholder="请输入任务编号" show-clear :disabled="readOnly" />
            </el-form-item>
            <el-form-item label="任务名称" prop="title">
              <el-input v-model="taskForm.title" placeholder="请输入任务名称" show-clear :disabled="readOnly" />
            </el-form-item>
            <el-form-item label="任务描述" prop="text">
              <el-input v-model="taskForm.text" placeholder="请输入任务描述" show-clear :disabled="readOnly" />
            </el-form-item>
            <el-form-item label="父任务" prop="parentTaskId">
              <el-input v-model="taskForm.parentTaskId" placeholder="无父任务" show-clear :disabled="true" />
            </el-form-item>
            <el-form-item label="计划开始时间" prop="planBegin">
              <el-date-picker
                v-model="taskForm.planBegin"
                type="datetime"
                placeholder="请选择计划开始时间"
                show-clear
                :disabled="readOnly"
                format="yyyy-MM-dd HH:mm"
                value-format="yyyy-MM-dd HH:mm"
              />
            </el-form-item>
            <el-form-item label="计划结束时间" prop="planEnd">
              <el-date-picker
                v-model="taskForm.planEnd"
                type="datetime"
                placeholder="请选择计划结束时间"
                show-clear
                :disabled="readOnly"
                format="yyyy-MM-dd HH:mm"
                value-format="yyyy-MM-dd HH:mm"
              />
            </el-form-item>
            <el-form-item label="实际开始时间" prop="beginTime">
              <el-date-picker
                v-model="taskForm.beginTime"
                type="datetime"
                placeholder="请选择实际开始时间"
                show-clear
                :disabled="readOnly"
                format="yyyy-MM-dd HH:mm"
                value-format="yyyy-MM-dd HH:mm"
              />
            </el-form-item>
            <el-form-item label="实际结束时间" prop="endTime">
              <el-date-picker
                v-model="taskForm.endTime"
                type="datetime"
                placeholder="请选择实际结束时间"
                show-clear
                :disabled="readOnly"
                format="yyyy-MM-dd HH:mm"
                value-format="yyyy-MM-dd HH:mm"
              />
            </el-form-item>
            <el-form-item label="完成百分比" prop="progress">
              <el-input-number
                v-model="taskForm.progress"
                :min="0"
                :max="100"
                placeholder="请输入完成百分比"
                show-clear
                :disabled="readOnly"
              />
            </el-form-item>
            <el-form-item label="优先级" prop="level">
              <el-select v-model="taskForm.level" placeholder="请选择优先级" :disabled="readOnly">
                <el-option v-for="item in levelList" :key="item.value" :label="item.text" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-form>
        </el-collapse-item>
        <el-collapse-item name="2">
          <template #title>
            <div class="coll-title">资源信息</div>
            <el-button
              title="编辑"
              :disabled="readOnly"
              size="mini"
              icon="el-icon-edit"
              circle
              @click.stop="openTaskInfo"
            ></el-button>
          </template>
          <el-table :data="taskForm.taskSourseDTOList" border style="width: 100%" :show-header="false">
            <el-table-column
              prop="index"
              label="#"
              width="50"
              :formatter="(row, column, cellValue, index) => index + 1"
            />
            <el-table-column prop="sourseName" label="资源名称" />
            <el-table-column prop="type" label="资源类型" />
            <el-table-column
              prop="responsible"
              label="主要资源"
              :formatter="(row, column, cellValue, index) => (cellValue === 1 ? '主要责任人' : '次要资源')"
            />
          </el-table>
        </el-collapse-item>
      </el-collapse>
    </div>
    <div class="config-btn">
      <el-button style="margin-right: 10px" type="primary" :disabled="readOnly" @click="submitTask">确定</el-button>
      <el-button @click="$emit('cancel')">关闭</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "taskConfig",
  data() {
    return {
      activeName: "1",
      taskForm: {},
      levelList: [],
      personList: [],
      rules: {
        code: [{ required: true, message: "请输入任务编号", trigger: "blur" }],
        title: [{ required: true, message: "请输入任务名称", trigger: "blur" }],
        text: [{ required: true, message: "请输入任务描述", trigger: "blur" }],
        planBegin: [{ required: true, message: "请选择计划开始时间", trigger: "blur" }],
        planEnd: [{ required: true, message: "请选择计划结束时间", trigger: "blur" }],
      },
    };
  },
  computed: {
    readOnly() {
      if (this.taskForm.state === "完成") {
        return true;
      } else return false;
    },
    hasTaskSourseDTO() {
      if (this.taskForm.taskSourseDTOList) {
        return this.taskForm.taskSourseDTOList.length;
      } else return 0;
    },
  },
  props: {
    taskInfoDate: {
      type: Object,
      default: () => ({}),
    },
  },
  watch: {
    taskInfoDate: {
      immediate: true,
      handler(newVal) {
        this.taskForm = newVal;
      },
      deep: true,
    },
  },
  mounted() {
    this.getLevelList();
    this.getPersonList();
  },
  methods: {
    getPersonList() {
      this.$server()
        .query("function-user", "DepartmentUser", {}, null, null, false)
        .then((res) => {
          this.personList = res.list.map((item) => {
            const obj = {};
            obj.name = item.data.name;
            obj.value = item.data.id;
            obj.id = item.data.id;
            obj.department = item.department.title;
            return obj;
          });
        })
        .catch((err) => {
          this.$notify(err.data.message, "error");
        });
    },
    // 优先级
    getLevelList() {
      this.$server()
        .batchQueryDictionary("任务优先级")
        .then((res) => {
          const result = res[0]?.details || [];
          this.levelList = result.map((item) => ({ text: item.v, value: item.k }));
        })
        .catch((err) => {
          this.$notify(err.data.message, "error");
        });
    },
    submitTask() {
      this.$emit("refreshTask", this.taskForm);
      this.$emit("cancel");
    },
    openTaskInfo() {
      this.$emit("openTaskDetail", this.taskForm.id);
    },
  },
};
</script>

<style scoped>
.coll-title {
  font-size: 16px;
  font-weight: 600;
}
.config-btn {
  margin-top: 15px;
  float: right;
}
</style>